import {updateWXInfo} from '../api/index'

export const getUserInfo = (toAuth) => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success ({authSetting}) {
        if (authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success ({userInfo}) {
              if (userInfo) {
                updateWXInfo(userInfo.nickName, userInfo.avatarUrl).then(data => {
                  resolve(data)
                  console.log('更新用户信息成功')
                }).catch(err => {
                  reject()
                  console.log('更新用户信息失败')
                })
              } else {
                reject()
              }
            },
            fail () {
              console.log('拒绝了')
              reject()
            }
          })
        } else {
          if (toAuth) {
            wx.navigateTo({
              url: '/pages/index/get-wx-auth'
            })
          }
          reject()
        }
      }
    })
  })
}
